package com.ftg.learn.chapter10.example02;

import com.ftg.learn.chapter10.example01.Stu;

import java.lang.reflect.TypeVariable;
import java.util.Arrays;

/**
 * javadoc 文档
 * java javac javadoc
 * 测试 功能
 *
 * @param <R>
 * @param <X>
 * @author kangjx
 * @version 1.0
 * @date 2023/2/2 下午1:59
 */
public class DemoPlay<R, X> {

    public DemoPlay(){
        TypeVariable<? extends Class<? extends DemoPlay>>[] c1 = this.getClass().getTypeParameters();
        System.out.println(Arrays.toString(c1));
    }

    R r1;
    Object obj;
    public R[] abc(){
        R r1 = (R) new Object();
        TypeVariable<? extends Class<? extends DemoPlay>>[] c1 = this.getClass().getTypeParameters();
        System.out.println(Arrays.toString(c1));
        return null;
    }

    /**
     * 泛型出现三个地方
     * 全局变量
     * 方法参数
     * 方法返回 值
     */
//    T 返回 值
//    E 参数
//    MyFunction<T,E>
//    public T findFirstStuNyName(T[] stus,MyFunction<T,T> mf){
//        return mf.apply();
//    }

//    public interface MyFunction<T,E> {
//
//        T apply(E x);
//
//    }

//    public X findFristStuByName(X[] stus, MyFunction<X,X> mf){
//        for (X x : stus) {
//            return mf.apply(x);
//        }
//        return null;
//    }

    public R findFristStuByName(X[] stus, MyFunction<R, X> mf) {
        for (X x : stus) {
            return mf.apply(x);
        }
        return null;
    }

    public static void main(String[] args) {
        Stu[] stus = new Stu[2];
        Stu s1 = new Stu();
        s1.setName("张三");
        s1.setAge(23);
        s1.setSex("不祥");
        stus[0] = s1;
        Stu s21 = new Stu();
        s21.setName("张四");
        s21.setAge(23);
        s21.setSex("不祥");
        stus[1] = s21;

//        var x;
        //jvm -

        //mybatis
        DemoPlay<ReturnStu, Stu> dp1 = new DemoPlay<>();
        //lambda 推送出来X为stu，所以x可以点出来东西
        ReturnStu name = dp1.findFristStuByName(stus, x -> {
            if (x.getName().startsWith("张")) {
                ReturnStu rs = new ReturnStu();
                rs.setName(x.getName());
                rs.setSex(x.getSex());
                return rs;
            }
            return null;
        });
        System.out.println(name);
        dp1.abc();
    }

}
